local zishucup__yuanjiang = fk.CreateSkill{
  name = "zishucup__yuanjiang",
}

Fk:loadTranslationTable{
  ["zishucup__yuanjiang"] = "远疆",
  [":zishucup__yuanjiang"] = "你手牌数减少的阶段结束后，可以亮出牌堆顶的一张牌。若如此做，当前回合角色可以将之对你使用，否则你获得之。",

  ["@zishucup__yuanjiang-phase"] = "远疆",
  ["#zishucup__yuanjiang-use"] = "远疆：你可以对 %dest 使用亮出的牌，否则其获得之！",

  ["$zishucup__yuanjiang1"] = "鲜卑兵犯，即刻诛灭，头悬街市震慑蛮夷!",
  ["$zishucup__yuanjiang2"] = "乌桓王叛，就地斩杀，来日我自奏明魏王!",
}

zishucup__yuanjiang:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(zishucup__yuanjiang.name) and player:getMark("@zishucup__yuanjiang-phase") ~= 0
    and player:getHandcardNum() < tonumber(player:getMark("@zishucup__yuanjiang-phase"))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(1)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonPut,
      skillName = zishucup__yuanjiang.name,
      proposer = player
    })
    if #cards > 0 then
      if (not room.current or room.current.dead or not room.current:canUseTo(Fk:getCardById(cards[1]), player, {bypass_times = true})) and not player.dead then
        room:obtainCard(player, cards, true, fk.ReasonJustMove, player, zishucup__yuanjiang.name)
      elseif room.current and not room.current.dead and not player.dead and room.current:canUseTo(Fk:getCardById(cards[1]), player, {bypass_times = true}) then
        local use = room:askToUseRealCard(room.current, {
        pattern = cards,
        skill_name = zishucup__yuanjiang.name,
        prompt = "#zishucup__yuanjiang-use::"..player.id,
        extra_data = {
          bypass_times = true,
          extraUse = true,
          expand_pile = cards,
          exclusive_targets = {player.id},
        },
        skip = true,
        cancelable = true,
        })
        if use then
          room:useCard{
            card = use.card,
            from = room.current,
            tos = {player},
            skillName = zishucup__yuanjiang.name,
            extraUse = true,
            subTos = use.subTos or {},
          }
        elseif not use and not player.dead then
          room:obtainCard(player, cards, true, fk.ReasonJustMove, player, zishucup__yuanjiang.name)
        end
      end
    else
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonJustMove, zishucup__yuanjiang.name)
    end
  end,
})

zishucup__yuanjiang:addEffect(fk.EventPhaseStart, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(zishucup__yuanjiang.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@zishucup__yuanjiang-phase", tostring(player:getHandcardNum()))
  end,
})

return zishucup__yuanjiang
